<?php

namespace App\Http\Controllers\Owner;

use App\Models\Member;
use App\Models\Order;
use App\Models\Shipping;
use Illuminate\Http\Request;
use App\Http\Controllers\Owner\BaseController;
use Illuminate\Support\Facades\Cache;

class IndexController extends BaseController
{
    //
    /**
     * 后台布局
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function layout()
    {

        $phone = $this->owner_phone;
        return view('owner.layout')->with('phone', $phone);
    }

    public function index(Request $request)
    {

        // 系统信息
        $sys_info = cache('sys_cache_server_info');
        if (!$sys_info) {
            $sys_info = $this->getServerInfo();
            cache(['sys_cache_server_info' => $sys_info], 10 * 60);
        }

        // 统计数据
        $driver = Member::where('role_id', Member::ROLE_DRIVER)->count();
        $owner = Member::where('role_id', Member::ROLE_OWNER)->count();
        $order = Order::count();
        $shipping = Shipping::count();

        return view('owner.index.index', compact('sys_info', 'driver', 'owner', 'order', 'shipping'));

    }

    /**
     * 获取系统信息
     * @return mixed
     */
    protected function getServerInfo()
    {
        $sys_info['os'] = PHP_OS;
        $sys_info['zlib'] = function_exists('gzclose') ? 'YES' : 'NO'; //zlib
        $sys_info['safe_mode'] = (boolean)ini_get('safe_mode') ? 'YES' : 'NO'; //safe_mode = Off
        $sys_info['timezone'] = function_exists("date_default_timezone_get") ? date_default_timezone_get() : "no_timezone";
        $sys_info['curl'] = function_exists('curl_init') ? 'YES' : 'NO';
        $sys_info['web_server'] = $_SERVER['SERVER_SOFTWARE'];
        $sys_info['phpv'] = phpversion();
        $sys_info['ip'] = GetHostByName($_SERVER['SERVER_NAME']);
        $sys_info['fileupload'] = @ini_get('file_uploads') ? ini_get('upload_max_filesize') : 'unknown';
        $sys_info['max_ex_time'] = @ini_get("max_execution_time") . 's'; //脚本最大执行时间
        $sys_info['domain'] = $_SERVER['HTTP_HOST'];
        $sys_info['memory_limit'] = ini_get('memory_limit');
        $dbPort = config('database.prefix');
        $dbHost = config('database.prefix');
        $dbHost = empty($dbPort) || $dbPort == 3306 ? $dbHost : $dbHost . ':' . $dbPort;

        $musql_version = \DB::select('select version() as ver');
        $sys_info['mysql_version'] = $musql_version[0]->ver;
        if (function_exists("gd_info")) {
            $gd = gd_info();
            $sys_info['gdinfo'] = $gd['GD Version'];
        } else {
            $sys_info['gdinfo'] = "未知";
        }

        return $sys_info;
    }


    /**
     * 清空缓存
     * @return mixed
     */
    public function flush()
    {
        Cache::flush();
        return $this->success('缓存已清除');
    }

}
